import Vue from 'vue'
import Router from 'vue-router'
import E404 from '../component/error/404';
import Blank from '../component/blank/Blank';

import authorityService from '../service/AuthorityService.js';
import {eventHub} from '../service/EventHub';


//路由配置
Vue.use(Router)

const router = new Router({
  routes: [
    {path: '/', redirect: {name: "blank"}},
    {path: '/404', name: '404', component: E404},
    {path: '/blank', name: 'blank', component: Blank}
  ]
})

router.beforeEach((to, from, next) => {
  let toPath = to.name ? to.name : "index";
  if (!authorityService.checkAuthority(toPath)) {
    error('权限不足，请联系管理员。处理完后请重新登录！');
    router.push({name: "index"})
  } else {
    next()
  }
})

/**
 * 配置权限忽略地址
 */
authorityService.addIgnorePath("index")
authorityService.addIgnorePath("404");
authorityService.addIgnorePath("blank");
window.error = window.alert;

eventHub.$on("404", (d) => {
  router.push({name: "404"})
})


export default router
